package com.synnex.mobile.servier.core.actRecord.service.impl;

import com.synnex.mobile.generic.core.util.ObjectUtil;
import com.synnex.mobile.servier.core.actRecord.bo.ActivityRecordBo;
import com.synnex.mobile.servier.core.actRecord.bo.FolderBo;
import com.synnex.mobile.servier.core.actRecord.dao.ActRecordDao;
import com.synnex.mobile.servier.core.actRecord.dao.FolderDao;
import com.synnex.mobile.servier.core.actRecord.dto.ActivityRecord;
import com.synnex.mobile.servier.core.actRecord.dto.Folder;
import com.synnex.mobile.servier.core.actRecord.service.ActRecordService;
import com.synnex.mobile.servier.core.service.impl.AbstractServiceImpl;
import com.synnex.mobile.servier.web.utils.DateUtil;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: kerryf
 * Date: 13-8-5
 * To change this template use File | Settings | File Templates.
 */
@Transactional
public class ActRecordServiceImpl extends AbstractServiceImpl implements ActRecordService {

    private ActRecordDao actRecordDao;
    private FolderDao folderDao;

    public void setFolderDao(FolderDao folderDao) {
        this.folderDao = folderDao;
    }

    public void setActRecordDao(ActRecordDao actRecordDao) {
        this.actRecordDao = actRecordDao;
    }

    public List<ActivityRecordBo> ListActRecord(String key, String category) {
        List<ActivityRecord> recordList = actRecordDao.ListActRecord(key, category);
        List<Folder> folders = folderDao.ListFolder(category);

        List<ActivityRecordBo> bos =  ActivityRecordBo.createInstance(recordList);

        for(ActivityRecordBo bo: bos){
            for(Folder f:folders){
                if(bo.getFolderId().intValue() == f.getId().intValue()){
                    bo.setFolderName(f.getTitle());
                    break;
                }
            }
        }

        return bos;

    }

//    public List<Map<String, Object>> listRecordToClient(String category) {
//
//        List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
//
//
//        List<Date> dateList = actRecordDao.getDate(category);
//        List<ActivityRecord> recordList = actRecordDao.ListActRecord(null, category);
//        List<ActivityRecordBo> bos = ActivityRecordBo.createInstance(recordList);
//
//        for (Date date : dateList) {
//            Map<String, Object> data = new HashMap<String, Object>();
//            data.put("date", DateUtil.dateToStr(date, "yyyy-MM-dd"));
//            data.put("title", DateUtil.dateToStr(date, "dd"));
//            List<ActivityRecordBo> boForDate;
//            if (bos != null && !bos.isEmpty()) {
//                boForDate = new ArrayList<ActivityRecordBo>();
//                for (ActivityRecordBo bo : bos) {
//                    if (DateUtil.compare(date, bo.getActivityDay()) == 0) {
//                        boForDate.add(bo);
//                    }
//                }
//                data.put("pdfList", boForDate);
//            }
//            dataList.add(data);
//        }
//
//
//        return dataList;
//    }

    public List<Map<String, Object>> listRecordToClient(String category) {

        List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();


//        List<Date> dateList = actRecordDao.getDate(category);
        List<Folder> folders = folderDao.ListFolder(category);
        List<ActivityRecord> recordList = actRecordDao.ListActRecord(null, category);
        List<ActivityRecordBo> bos = ActivityRecordBo.createInstance(recordList);

        for (Folder folder : folders) {
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("date", folder.getDate());
            data.put("title", folder.getTitle());
            List<ActivityRecordBo> boForFolder;
            if (bos != null && !bos.isEmpty()) {
                boForFolder = new ArrayList<ActivityRecordBo>();
                for (ActivityRecordBo bo : bos) {
                    if (bo.getFolderId().intValue()==folder.getId().intValue()) {
                        boForFolder.add(bo);
                    }
                }
                data.put("pdfList", boForFolder);
            }
            dataList.add(data);
        }


        return dataList;
    }

    public void addActRecord(ActivityRecordBo recordBo) {
        ActivityRecord record = ObjectUtil.copyProperties(recordBo, ActivityRecord.class);
        record.setEntryDate(new Date());

        actRecordDao.addActRecord(record);
    }

    public void editActRecord(ActivityRecordBo recordBo) {
        ActivityRecord record = ObjectUtil.copyProperties(recordBo, ActivityRecord.class);
        record.setUpdateDate(new Date());

        actRecordDao.editActRecord(record);
    }

    public ActivityRecordBo getActRecordBoById(Integer id) {

        ActivityRecord contact = actRecordDao.getActRecordById(id);
        return ActivityRecordBo.createInstance(contact);
    }

    public void deleteActRecordById(Integer id) {
        actRecordDao.deleteActRecordById(id);
    }


    /////////////////

    public List<FolderBo> ListFolder(String category) {
        List<Folder> folders = folderDao.ListFolder(category);
        return FolderBo.createInstance(folders);
    }

    public void addFolder(FolderBo folderBo) {
        Folder folder = ObjectUtil.copyProperties(folderBo, Folder.class);
        folder.setEntryDate(new Date());
        folderDao.addFolder(folder);
    }


    public void editFolder(FolderBo folderBo) {
        Folder folder = ObjectUtil.copyProperties(folderBo, Folder.class);
        folder.setUpdateDate(new Date());
        folderDao.editFolder(folder);
    }

    public FolderBo getFolderBoById(Integer id) {
        return FolderBo.createInstance(folderDao.getFolderById(id));
    }

    public void deleteFolderBoById(Integer id) {
        folderDao.deleteFolderById(id);
    }


}
